<?php

namespace Home\Controller;

use Think\Controller;

class LoginController extends Controller {

    public function index() {
        $c_userid = cookie('userid');
        if ($c_userid > 0) {
            $info = M("user")->field("name")->where(" id= '" . $c_userid . "'")->find();
            if (!empty($info['name'])) {
                session('username', $info['name']);
                session('userid', $c_userid);
            }
        }
        if (session('userid') > 0) {
            $url = C('SITE_URL');
            if (cookie("area") > 0) {
                $url = U("Area/lists", array("id" => cookie("area")));
            }
            echo "<script>document.location.href='" . $url . "';</script>";
        }
       $detail = json_decode(getTableField("shortMessage", "three"),true);

        
        if ($detail['is_phone_bind'] == 1) {
            $searchWord = "请输入用户名/邮箱/已验证的手机";
        } else {
            $searchWord = "请输入用户名/邮箱";
        }
        $this->assign("ctitle", '用户登录');
        $this->assign("action", CONTROLLER_NAME);
        $this->assign("config", getTableConfig());
        $this->assign("searchWord", $searchWord);
        $threeLogin = getTableFile("accounts");
        $this->assign("threeLogin", $threeLogin);
        $this->display("Index:login");
    }

    public function login() {
        $name = I('post.name');
        $pwd = I('post.pwd');
        $info = M('user')->field("id,name")->where("(name='" . $name . "' OR email ='" . $name . "' OR phone ='" . $name . "') AND pwd = '" . md5($pwd) . "'")->find();

//      echo $Dao->getLastSql()."<hr>";

        if ($info['id'] > 0) {
            if (I("post.is_auto") == 1) {
                cookie('userid', $info['id']);
            }
            session('username', $info['name']);
            session('userid', $info['id']);
            $openid = session("openid");
            $field = session('type');
            $returnUrl = session('returnUrl');
            if ($returnUrl) {
                session('returnUrl', null);
            } else {
                if (cookie("area") > 0) {
                    $returnUrl = U("Area/lists", array("id" => cookie("area")));
                } else {
                    $returnUrl = C('SITE_URL');
                }
            }
            if (session('type') != '') {
                $num = M('user')->field('id')->where("" . $field . "= " . $openid . "")->count();
                if ($num > 0) {
                    $this->error("已被其他账户绑定！", $returnUrl);
                    exit;
                }
                $info2 = $Dao->where("id= " . $info['id'] . "")->find();
//                echo $Dao->getLastSql();exit;
                if ($info2[session('type')] == '') {
                    $data[$field] = $openid;

                    $config = getTableConfig();
                    $points = $config["points_" . $field];
                    M('user')->where("id=" . session('userid') . "")->setInc('money', $points);
                    addLog("bind_three", $points, session('userid'), "绑定" . session('type') . "获得" . $points . "积分", 5, 1);
                    session('type', null);
                    session('openid', null);
//        echo $Dao->getLastSql();exit;
                } else {
                    $this->error("您的账号已绑定！", $returnUrl);
                    exit;
                }
            }
            $data['loginip'] = get_client_ip();
            $data['logintime'] = time();
            M('user')->where("id = " . $info['id'] . "")->save($data);
//          echo $Dao->getLastSql();exit;

            session("order", null); //清除未登陆
            echo "<script>document.location.href='" . $returnUrl . "';</script>";
            //$this->success('登陆成功!', $returnUrl);
        } else {
            $this->error('用户名或密码不正确!');
        }
    }

}

?>
